package xyz.riceball.develop.service.impl;

import cn.hutool.db.DbUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import xyz.riceball.develop.common.enums.ENFieldParamType;
import xyz.riceball.develop.entity.po.InoutParamPO;
import xyz.riceball.develop.entity.po.RoutePO;
import xyz.riceball.develop.entity.po.SqlDefinitionPO;
import xyz.riceball.develop.mapper.DevelopRouteMapper;
import xyz.riceball.develop.mapper.InoutParamMapper;
import xyz.riceball.develop.mapper.SqlDefinitionMapper;
import xyz.riceball.develop.service.CommonService;
import xyz.riceball.framework.core.bean.ApiR;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *
 * </p>
 *
 * @author xiaovcloud
 * @since 2022/7/17 10:00
 */
@Service
public class CommonServiceImpl implements CommonService {

    @Resource
    private DevelopRouteMapper developRouteMapper;
    @Resource
    private SqlDefinitionMapper sqlDefinitionMapper;
    @Resource
    private InoutParamMapper inoutParamMapper;

    @Override
    public ApiR executeQuery(Map<String, Object> params, String routeId) {
        //查出功能
        RoutePO routePO = developRouteMapper.selectById(routeId);
        if (routePO == null) {
            return null;
        }

        //查询sql定义
        LambdaQueryWrapper<SqlDefinitionPO> wrapperSql = Wrappers.lambdaQuery();
        wrapperSql.eq(SqlDefinitionPO::getRouteId, routeId);
        SqlDefinitionPO sqlDefinitionPO = sqlDefinitionMapper.selectOne(wrapperSql);
        if (sqlDefinitionPO == null) {
            return null;
        }

        //输入参数
        LambdaQueryWrapper<InoutParamPO> wrapperIn = Wrappers.lambdaQuery();
        wrapperIn.eq(InoutParamPO::getFieldType, ENFieldParamType.IN);
        List<InoutParamPO> inParamList = inoutParamMapper.selectList(wrapperIn);
        //输出参数
        LambdaQueryWrapper<InoutParamPO> wrapperOut = Wrappers.lambdaQuery();
        wrapperOut.eq(InoutParamPO::getFieldType, ENFieldParamType.IN);
        List<InoutParamPO> outParamList = inoutParamMapper.selectList(wrapperOut);

        String sqlText = sqlDefinitionPO.getSqlText();

//        DbUtil.


        return ApiR.okMsg("success");
    }
}
